﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Messaging;

namespace PackerService
{
    public partial class Service1 : ServiceBase
    {
        const string recieveQueue = ".\\private$\\projectSpamQueue";
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            MessageQueue msMq = new MessageQueue(recieveQueue);
            msMq.Formatter = new XmlMessageFormatter(new Type[] { typeof(List<string>) });
            Packer packer = new Packer();
            while (true)
            {
                var message = (List<string>)msMq.Receive().Body;
                string command = message.First();
                message.Remove(message.First());
                switch (command)
                { 
                        //to backup
                    case "666":
                        bool compress;
                        string name = message.First();
                        message.Remove(message.First());
                        Boolean.TryParse(message.First(),out compress);
                        message.Remove(message.First());
                        string path = message.First();
                        message.Remove(message.First());
                        packer.backUpFiles(compress, name, path,message);
                        break;
                        //to getinfo
                    case "13":
                        List<BackUpInfo> backUps = packer.getBackUps();
                        msMq.Send(backUps);
                        break;
                        //to recover
                    case "7":
                        bool compress2;
                        string name2 = message.First();
                        message.Remove(message.First());
                        Boolean.TryParse(message.First(),out compress2);
                        message.Remove(message.First());
                        string root = message.First();
                        message.Remove(message.First());
                        packer.unPack(root,name2,compress2,message);
                        break;
                }
            }
        }

        protected override void OnStop()
        {
        }
    }
}
